namespace Admin2025.Domain.Entities.App;

/// <summary>
/// 应用用户实体。
/// </summary>
public class AppUser : EntityBase
{
    /// <summary>
    /// 用户名
    /// </summary>
    public string Username { get; set; } = null!;

    /// <summary>
    /// 密码
    /// </summary>
    public string Passwprd { get; set; } = null!;

    /// <summary>
    /// 密码盐，随机生成字符串，用于加密
    /// </summary>
    public string Salt { get; set; } = null!;

    /// <summary>
    /// 所属部门ID
    /// </summary>
    public Guid? DepartmentId { get; set; }

    /// <summary>
    /// 所属部门实体
    /// </summary>
    public AppDepartment? Department { get; set; }

    /// <summary>
    /// 用户关联的角色集合
    /// </summary>
    public ICollection<AppRole> Roles { get; set; } = new List<AppRole>();

    /// <summary>
    /// 分配角色
    /// </summary>
    /// <param name="role">要分配的角色</param>
    public void AssignRole(AppRole role)
    {
        if (!Roles.Contains(role))
        {
            Roles.Add(role);
        }
    }

    /// <summary>
    /// 移除角色
    /// </summary>
    /// <param name="role">要移除的角色</param>
    public void RemoveRole(AppRole role)
    {
        if (Roles.Contains(role))
        {
            Roles.Remove(role);
        }
    }

    /// <summary>
    /// 判断用户是否拥有指定权限编码的权限。
    /// </summary>
    /// <param name="permissionCode">权限编码。</param>
    /// <returns>如果用户拥有该权限，返回 true；否则返回 false。</returns>
    public bool HasPermission(string permissionCode)
    {
        return Roles.Any(r => r.HasPermission(permissionCode));
    }
}